//
//  ResetPwdStep.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/5/18.
//

import Combine

struct CDResetPwdStep: LoginStep {
    struct InputData {
        let userName: String
        let password: String
    }

    func execute(_ input: InputData) -> AnyPublisher<AuthAPI.UpdatePwd.Response, Error> {
        Future { promise in
            let api = AuthAPI.UpdatePwd(body: .init(userName: input.userName, newPassword: input.password))
            NetworkManager.shared.send(api, completion: { result in
                switch result {
                case .success(let response):
                    if let response = response.data
                    {
                        Logger.log(.info, message: "修改密码成功: \(response)",tag: .login)
                        promise(.success(response))
                    }
                    else{
                        promise(.failure(NSError(domain: "修改密码", code: response.code as! Int, userInfo: [NSLocalizedDescriptionKey: response.msg ?? "无错误描述"])))
                    }
                case .failure(let error):
                    promise(.failure(error))
                }
            })
        }
        .eraseToAnyPublisher()
    }
}
